#include <IRremote.h>
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

int motorLA = 10;  // left A
int motorLB = 11;  // left B
int motorRA = 8;   // right A
int motorRB = 9;   // right B

int irVCC = 7;
int irGND = 6;
int irSignal = 5; // place ir sensor window outwards



IRrecv irrecv(irSignal);
decode_results results;


// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
  
  
  
  pinMode(irGND,OUTPUT); digitalWrite(irGND, LOW);
  pinMode(irVCC,OUTPUT); digitalWrite(irVCC,HIGH);
  
  pinMode(irSignal,INPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);

}

void blink() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);  

}


void stopp() {
  digitalWrite(motorLA,HIGH);
  digitalWrite(motorLB,HIGH);

  digitalWrite(motorRA,HIGH);
  digitalWrite(motorRB,HIGH);  
}

void ahead() {
  digitalWrite(motorLA,HIGH);
  digitalWrite(motorLB,LOW);
  
  digitalWrite(motorRA,HIGH);
  digitalWrite(motorRB,LOW);
  
}

void right() {
  digitalWrite(motorLA,HIGH);
  digitalWrite(motorLB,LOW);

  digitalWrite(motorRA,HIGH);
  digitalWrite(motorRB,HIGH);  
}

void left() {
  digitalWrite(motorLA,HIGH);
  digitalWrite(motorLB,HIGH);
  
  digitalWrite(motorRA,HIGH);
  digitalWrite(motorRB,LOW);

}

void backwards() {
  digitalWrite(motorLA,LOW);
  digitalWrite(motorLB,HIGH);

  digitalWrite(motorRA,LOW);
  digitalWrite(motorRB,HIGH);
  
}
  
void testWheels() {
    blink();  
    blink();
    ahead();
    delay(1000);
    
    blink();
    stopp();
    delay(1000);

    blink();
    backwards();
    delay(1000);

    blink();
    stopp();
    delay(1000);
    
}


#define motor_forward 1
#define motor_stop 2
#define motor_back 3

#define motor_left 4
#define motor_right 5

void moveMotor(int command) {
  #define GIRO 400

  switch(command) {
    case motor_forward:
      Serial.println("move ahead ");
      ahead();
      delay(1000);
      stopp();
    break;
    case motor_back:
      Serial.println("move back ");
      backwards();
      delay(1000);
      stopp();
    break;
    case motor_right:
      right();
      delay(GIRO);
      stopp();
    break;
    case motor_left:
      left();
      delay(GIRO);
      stopp();
    break;
    default:
      stopp();
  }
  

}

int readIR() {
  
  results.value = 0;
  
  if (irrecv.decode(&results)) {
    if (results.decode_type == NEC) {
      Serial.print("NEC: ");
    } else if (results.decode_type == SONY) {
      Serial.print("SONY: ");
    } else if (results.decode_type == RC5) {
      Serial.print("RC5: ");
    } else if (results.decode_type == RC6) {
      Serial.print("RC6: ");
    } else if (results.decode_type == UNKNOWN) {
      Serial.print("UNKNOWN: ");
    }
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    
  }


  return results.value;
}

void testIR() {
  
  int val = readIR();
  
  switch (val) {
    case 0xFF32CD:
      moveMotor(motor_forward);
    break;
    case 0xFF00FF:
      moveMotor(motor_back);
    break;
    case 0xFFA857:
      moveMotor(motor_left);
    break;
    case 0xFFB04F:
      moveMotor(motor_right);
    break;

    default: moveMotor(motor_stop);
  }
}

// the loop routine runs over and over again forever:
void loop() {
//  testWheels();

  testIR();
}
